package com.iot.platform.mvc.thing.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iot.common.core.exception.ThingModelAbilityException;
import com.iot.common.core.util.R;
import com.iot.common.core.util.UuidUtil;
import com.iot.common.security.annotation.SecurityAuthorization;
import com.iot.platform.api.domain.ThingAbilityModule;
import com.jfinal.kit.StrKit;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@RestController
@RequiredArgsConstructor
@RequestMapping("/thing/ability/module")
@Api(value = "ThingAbilityController", tags = "功能")
public class ThingAbilityModuleController {

	@Setter
	private Throwable cause;

	@SecurityAuthorization
	@GetMapping(value = { "/getOptions" }, produces = "application/json; charset=utf-8")
	public R<List<ThingAbilityModule>> getOptions(
			@RequestParam(name = "productId", required = false, defaultValue = "") String productId,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords) throws Exception {

		String sqlExceptSelect = " FROM " + ThingAbilityModule.TABLE_NAME + " O  WHERE O.PRODUCT_ID='" + productId
				+ "' ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keywords + "%' OR   O.CODE LIKE '%" + keywords + "%' ) ";
		}
		sqlExceptSelect += "  ORDER BY   O.SORT ASC ";
		return R.ok(ThingAbilityModule.dao.find("select * " + sqlExceptSelect));
	}

	@SecurityAuthorization
	@PostMapping(value = { "/save" }, produces = "application/json; charset=utf-8")
	public R<ThingAbilityModule> save(@Valid ThingAbilityModule abilityModule) throws Exception {
		if (StrKit.isBlank(abilityModule.getId())) {
			abilityModule.setId(UuidUtil.getUUID());
			if (abilityModule.save()) {
				return R.ok();
			}
		}
		if (abilityModule.update()) {
			return R.ok();
		}
		return R.failed();
	}

	@SecurityAuthorization
	@RequestMapping(value = "/getById", method = RequestMethod.GET)
	public R<ThingAbilityModule> getById(
			@RequestParam(name = "id", required = false, defaultValue = "") String id) {
		ThingAbilityModule ability = ThingAbilityModule.dao.findById(id);
		if (ability == null) {
			throw new ThingModelAbilityException("数据不存在！");
		}
		return R.ok(ability);
	}

	@SecurityAuthorization
	@PostMapping(value = { "/deleteById" }, produces = "application/json; charset=utf-8")
	public R<Boolean> deleteById(@RequestParam(name = "id", required = true) String id) throws Exception {
		if (ThingAbilityModule.dao.deleteById(id)) {
			return R.ok(true);
		} else {
			return R.failed(false);
		}
	}

}
